package com.loveprogrammer.handler;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.base.network.support.BaseHandler;
import com.loveprogrammer.command.anotation.TagListener;
import com.loveprogrammer.command.anotation.TopicListener;
import com.loveprogrammer.command.client.ClientMenuTag;
import com.loveprogrammer.command.client.ClientTopic;
import com.loveprogrammer.command.server.ServerHeroTag;
import com.loveprogrammer.command.server.ServerTopic;
import com.loveprogrammer.common.SessionGameContext;
import com.loveprogrammer.constants.CommonValue;
import com.loveprogrammer.convert.HeroConvert;
import com.loveprogrammer.dto.base.StringRequest;
import com.loveprogrammer.dto.job.HeroCreate;
import com.loveprogrammer.dto.job.HeroInfo;
import com.loveprogrammer.factory.JobFactory;
import com.loveprogrammer.model.Hero;
import com.loveprogrammer.model.SessionAttributeKey;
import com.loveprogrammer.model.game.HeroWrapper;
import com.loveprogrammer.model.game.UserGameWrapper;
import com.loveprogrammer.pojo.StringMessage;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName HeroHandler
 * @Description 英雄角色相关的执行类
 * @Author admin
 * @Date 2024/2/5 15:49
 * @Version 1.0
 */
@Component
@TopicListener(topic = ServerTopic.TOPIC_HERO)
public class HeroHandler extends BaseHandler {

    public static final Logger log = LoggerFactory.getLogger(HeroHandler.class);

    @TagListener(tag = ServerHeroTag.TAG_HERO_CREATE,messageClass = HeroCreate.class)
    public StringMessage createHero(ChannelHandlerContext ctx, HeroCreate data) throws Exception {
        Integer jobId = data.getId();
        Hero hero = JobFactory.createHero(JobFactory.getJobMap().get(jobId), data.getHeroName());

        // 创建封装
        HeroWrapper wrapper = new HeroWrapper(hero);
        // 获取游戏封装
        int sessionId = getSessionId(ctx.channel());
        UserGameWrapper gameWrapper = SessionGameContext.USER_GAME_MAP.get(sessionId);
        // 角色名称作为key
        gameWrapper.getHeroWrapperMap().put(data.getHeroName(),wrapper);
        gameWrapper.setCurrentHero(wrapper);
        ctx.channel().attr(SessionAttributeKey.GAME).set(gameWrapper);
        // 角色创建成功，返回游戏主菜单


        StringMessage message = StringMessage.create(ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_PORTAL);
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
        StringRequest request = new StringRequest();
        request.setContent("英雄创建成功");
        message.setBody(JSON.toJSONString(request));
        return message;
    }

    @TagListener(tag = ServerHeroTag.TAG_HERO_INFO,messageClass = StringRequest.class)
    public StringMessage getHeroInfo(ChannelHandlerContext ctx, StringRequest data) throws Exception {
        int sessionId = getSessionId(ctx.channel());
        UserGameWrapper gameWrapper = SessionGameContext.USER_GAME_MAP.get(sessionId);
        HeroWrapper currentHero = gameWrapper.getCurrentHero();

        // 转成英雄详情
        HeroInfo heroInfo = HeroConvert.INSTANCE.toHeroInfo(currentHero.getHero());
        StringMessage message = StringMessage.create(data.getSuccessCallbackTopic(), data.getSuccessCallbackTag());
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
        message.setBody(JSON.toJSONString(heroInfo));
        return message;
    }
}
